package dal

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_admin_api/biz/model"
)

type ProductMaterialGroupDAO struct {
	bizID int64
}

type ProductMaterialGroupListParams struct {
	Condition []gen.Condition
	Page      int
	Size      int
	Sort      []field.Expr
}

func newProductMaterialGroupDAO(bizID int64) *ProductMaterialGroupDAO {
	return &ProductMaterialGroupDAO{
		bizID: bizID,
	}
}

func (c *ProductMaterialGroupDAO) MGetProductMaterialGroup(ctx context.Context, ids []int64) (map[int64]*model.ProductMaterialGroup, error) {
	list, err := ProductMaterialGroup.WithContext(ctx).Where(
		ProductMaterialGroup.BizID.Eq(c.bizID),
		ProductMaterialGroup.ID.In(ids...),
		ProductMaterialGroup.DeletedAt.IsNull(),
	).Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.MGetProductMaterialGroup err: %v", err)
		return nil, err
	}
	var data = make(map[int64]*model.ProductMaterialGroup)
	for _, info := range list {
		data[info.ID] = info
	}
	return data, nil
}

func (c *ProductMaterialGroupDAO) GetProductMaterialGroupByConditions(ctx context.Context, condition []gen.Condition) (*model.ProductMaterialGroup, error) {
	tx := ProductMaterialGroup.WithContext(ctx).Where(
		ProductMaterialGroup.BizID.Eq(c.bizID),
		ProductMaterialGroup.DeletedAt.IsNull(),
	)
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	info, err := tx.First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetProductMaterialGroupByConditions failed, %s", err.Error())
		return nil, err
	}
	return info, nil
}

func (c *ProductMaterialGroupDAO) GetProductMaterialGroupList(ctx context.Context, condition []gen.Condition, page, size int) ([]*model.ProductMaterialGroup, error) {
	tx := ProductMaterialGroup.WithContext(ctx).Where(ProductMaterialGroup.BizID.Eq(c.bizID), ProductMaterialGroup.DeletedAt.IsNull())
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	if page > 0 && size > 0 {
		tx = tx.Offset((page - 1) * size).Limit(size)
	}
	list, err := tx.Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetProductMaterialGroupList err: %v", err)
		return nil, err
	}
	return list, nil
}

func (c *ProductMaterialGroupDAO) GetProductMaterialGroupTotal(ctx context.Context, condition []gen.Condition) (int64, error) {
	total, err := ProductMaterialGroup.WithContext(ctx).Where(ProductMaterialGroup.BizID.Eq(c.bizID), ProductMaterialGroup.DeletedAt.IsNull()).Where(condition...).Count()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetProductMaterialGroupTotal error: %v", err)
		return 0, err
	}
	return total, nil
}

func (c *ProductMaterialGroupDAO) GetProductMaterialGroupInfoByID(ctx context.Context, id int64) (*model.ProductMaterialGroup, error) {
	info, err := ProductMaterialGroup.WithContext(ctx).Where(ProductMaterialGroup.BizID.Eq(c.bizID), ProductMaterialGroup.DeletedAt.IsNull()).Where(ProductMaterialGroup.ID.Eq(id)).First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetProductMaterialGroupInfoByID error: %v", err)
		return nil, err
	}
	return info, nil
}

func (c *ProductMaterialGroupDAO) GetListWithTotal(ctx context.Context, params *ProductMaterialGroupListParams) ([]*model.ProductMaterialGroup, int64, error) {
	tx := ProductMaterialGroup.WithContext(ctx).Where(ProductMaterialGroup.BizID.Eq(c.bizID), ProductMaterialGroup.DeletedAt.IsNull())
	if len(params.Condition) > 0 {
		tx = tx.Where(params.Condition...)
	}
	if len(params.Sort) > 0 {
		tx = tx.Order(params.Sort...)
	}
	list, total, err := tx.FindByPage((params.Page-1)*params.Size, params.Size)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.ProductMaterialGroup.GetListWithTotal err: %v", err)
		return nil, 0, err
	}
	return list, total, nil
}

func (c *ProductMaterialGroupDAO) CreateProductMaterialGroup(ctx context.Context, data *model.ProductMaterialGroup) (*model.ProductMaterialGroup, error) {
	if err := ProductMaterialGroup.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateProductMaterialGroup error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *ProductMaterialGroupDAO) CreateProductMaterialGroupWithTx(ctx context.Context, tx *Query, data *model.ProductMaterialGroup) (*model.ProductMaterialGroup, error) {
	if err := tx.ProductMaterialGroup.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateProductMaterialGroup error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *ProductMaterialGroupDAO) BatchCreateProductMaterialGroup(ctx context.Context, data []*model.ProductMaterialGroup) error {
	if err := ProductMaterialGroup.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateInBatchesProductMaterialGroup error: %v", err)
		return err
	}
	return nil
}

func (c *ProductMaterialGroupDAO) BatchCreateProductMaterialGroupWithTx(ctx context.Context, tx *Query, data []*model.ProductMaterialGroup) ([]*model.ProductMaterialGroup, error) {
	if err := tx.ProductMaterialGroup.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.BatchCreateWithTxProductMaterialGroup error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *ProductMaterialGroupDAO) UpdateProductMaterialGroup(ctx context.Context, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := ProductMaterialGroup.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateProductMaterialGroup error: %v", err)
		return err
	}
	return nil
}

func (c *ProductMaterialGroupDAO) UpdateProductMaterialGroupWithTx(ctx context.Context, tx *Query, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := tx.ProductMaterialGroup.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateProductMaterialGroup error: %v", err)
		return err
	}
	return nil
}

func (c *ProductMaterialGroupDAO) DeleteProductMaterialGroup(ctx context.Context, condition []gen.Condition) error {
	_, err := ProductMaterialGroup.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteProductMaterialGroup error: %v", err)
		return err
	}
	return nil
}

func (c *ProductMaterialGroupDAO) DeleteProductMaterialGroupWithTx(ctx context.Context, tx *Query, condition []gen.Condition) error {
	_, err := tx.ProductMaterialGroup.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteProductMaterialGroupWithTx error: %v", err)
		return err
	}
	return nil
}
